<?php

namespace App\Exceptions;

use Exception;

/**
 * ExitException represents a normal termination of an application.
 *
 * Do not catch ExitException. Laravel will handle this exception to terminate the application gracefully.
 */
class ExitException extends Exception
{
    /**
     * @var $data content
     */
    protected $data;

    /**
     * Constructor.
     *
     * @param string     $message  error message
     * @param int        $code     error code
     * @param \Exception $previous The previous exception used for the exception chaining.
     */
    public function __construct($code = 0, $message = null, \Exception $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    /**
     * Output and exit the app
     *
     * @return void
     */
    public function report()
    {
        // 添加跨域header
        $header = [
            'Access-Control-Max-Age'           => '3600',
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Headers'     => 'Origin, X-Requested-With, Content-Type, Accept',
            'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, OPTIONS',
            'Access-Control-Allow-Credentials' => 'false'
        ];

        response($this->data, 200, $header)->send();
        exit();
    }

    /**
     * 返回data数据
     *
     * @return content
     */
    public function getData()
    {
        return $this->data;
    }
}
